import axios from 'axios';
import qs from 'query-string';

export interface BizTicketId {
    id: number;
}

export interface BizTicketRecord {
    id: number;
    order_id: number;
    biz_goods_id: number;
    ticket_code: string;
    phone: string;
    goods_name: string;
    biz_name: string;
    status: number; // 0: 未使用, 1: 已使用, 2: 已过期
    used_at?: number;
    created_at: number;
    updated_at: number;
}

export interface BizTicketListRes {
    list: BizTicketRecord[];
    total: number;
}

export interface BizTicketModel {
    id: number;
    order_id: number;
    biz_goods_id: number;
    ticket_code: string;
    phone: string;
    goods_name: string;
    biz_name: string;
    status: number;
    used_at?: number;
}

export interface BizTicketParams extends Partial<BizTicketRecord> {
    current: number;
    pageSize: number;
    search?: string; // 搜索关键词
    phone?: string; // 手机号搜索
    goods_name?: string; // 商品名称搜索
    status?: number; // 状态筛选
}

/**
 * 获取核销码订单列表
 * @param params
 */
export function queryBizTicketList(params: BizTicketParams) {
    return axios.get<BizTicketListRes>('/admin/bizTicket/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取核销码订单详情
 * @param params
 */
export function queryBizTicketOne(params: BizTicketId) {
    return axios.get<BizTicketRecord>('/admin/bizTicket/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}



/**
 * 删除核销码订单
 * @param id
 */
export function delBizTicket(id: number) {
    return axios.post('/admin/bizTicket/delete', { id });
}

/**
 * 核销码使用
 * @param data
 */
export function useBizTicket(data: { id: number }) {
    return axios.post('/admin/bizTicket/use', data);
}

/**
 * 根据手机号和商品名称搜索核销码
 * @param params
 */
export function searchBizTicket(params: { phone?: string; goods_name?: string }) {
    return axios.get<BizTicketListRes>('/admin/bizTicket/search', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
} 